package com.HuiShou.controller.common;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.HuiShou.common.utils.LocationUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.HuiShou.common.annotation.Log;
import com.HuiShou.common.core.controller.BaseController;
import com.HuiShou.common.core.domain.AjaxResult;
import com.HuiShou.common.enums.BusinessType;
import com.HuiShou.domain.CollectionPoint;
import com.HuiShou.service.ICollectionPointService;
import com.HuiShou.common.utils.poi.ExcelUtil;
import com.HuiShou.common.core.page.TableDataInfo;

/**
 * 回收站点信息Controller
 * 
 * @author lyz
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/point/point")
public class CollectionPointController extends BaseController
{
    @Autowired
    private ICollectionPointService collectionPointService;

    /**
     * 查询回收站点信息列表
     */
    @PreAuthorize("@ss.hasPermi('point:point:list')")
    @GetMapping("/list")
    public TableDataInfo list(CollectionPoint collectionPoint)
    {
        startPage();
        List<CollectionPoint> list = collectionPointService.selectCollectionPointList(collectionPoint);
        list.forEach(e->e.setDistance(LocationUtil.getDistance(e.getLongitude(),e.getLatitude(),e.getUserLongitude(),e.getUserLatitude())));

        // 使用nullsLast处理null值
        List<CollectionPoint> sortedList = list.stream()
                .sorted(Comparator.comparing(
                        CollectionPoint::getDistance,
                        Comparator.nullsLast(Comparator.naturalOrder())
                ))
                .collect(Collectors.toList());
        return getDataTable(list);
    }

    /**
     * 导出回收站点信息列表
     */
    @PreAuthorize("@ss.hasPermi('point:point:export')")
    @Log(title = "回收站点信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CollectionPoint collectionPoint)
    {
        List<CollectionPoint> list = collectionPointService.selectCollectionPointList(collectionPoint);
        ExcelUtil<CollectionPoint> util = new ExcelUtil<CollectionPoint>(CollectionPoint.class);
        util.exportExcel(response, list, "回收站点信息数据");
    }

    /**
     * 获取回收站点信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('point:point:query')")
    @GetMapping(value = "/{pointId}")
    public AjaxResult getInfo(@PathVariable("pointId") String pointId)
    {
        return success(collectionPointService.selectCollectionPointByPointId(pointId));
    }

    /**
     * 新增回收站点信息
     */
    @PreAuthorize("@ss.hasPermi('point:point:add')")
    @Log(title = "回收站点信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CollectionPoint collectionPoint)
    {
        return toAjax(collectionPointService.insertCollectionPoint(collectionPoint));
    }

    /**
     * 修改回收站点信息
     */
    @PreAuthorize("@ss.hasPermi('point:point:edit')")
    @Log(title = "回收站点信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CollectionPoint collectionPoint)
    {
        return toAjax(collectionPointService.updateCollectionPoint(collectionPoint));
    }

    /**
     * 删除回收站点信息
     */
    @PreAuthorize("@ss.hasPermi('point:point:remove')")
    @Log(title = "回收站点信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pointIds}")
    public AjaxResult remove(@PathVariable String[] pointIds)
    {
        return toAjax(collectionPointService.deleteCollectionPointByPointIds(pointIds));
    }
}
